/*!
 * 面板控件.
 * ecp.qzz.ui.design.base.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.panel"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui.design");
	/**
	 * 面板控件.
	 */
	qzz.ui.design.base = qzz.Class(qzz.ui.panel, {
		/**
		 * 创建函数
		 */
	    _create:function() {
	    	this.base();
            this.className = "Base";
            this._selected = false;
            this.getOption().scale = this._getNumberValue(this.getOption().scale, 1);
	    },
	    /**
	     * 渲染.
	     */
	    _render:function() {
	    	if(this.getOption().borderStyle == null) {
	    		this.getOption().borderStyle = "dashed";
	    	}
	    	if(this.getOption().borderColor == null){
	    		this.getOption().borderColor = "#AAAAAA";
	    	}
	    	this.base();
	    	if(this.getOption().lock == "absolute") {
	    		this.thisWindow.style.position = "absolute";
	    	}
	    	this.getOption().top = this._getNumberValue(this.getOption().top, this.getOption().top);
	    	this.getOption().left = this._getNumberValue(this.getOption().left, this.getOption().left);
	    	if(this.isPecent(this.getOption().width) === false) {
	    	    this.getOption().width = this._getNumberValue(this.getOption().width, this.getOption().width);
	    	}	    	
	    	this.getOption().height = this._getNumberValue(this.getOption().height, this.getOption().height);
	    	//this.refresh();
	    },
	    
	    refreshTop:function() {
	    	/*
	    	 * releative 时，top 间距不符合要求
	    	 */
	    	if(this.parent != null && this.getOption().lock != "absolute") {
	    		var childList = this.parent.childComponent;
	    	    if(childList != null && childList.length > 0) {
	    	    	var ci = -1;
	    	    	for(var i = 0, len = childList.length; i < len; i++) {
	    	    		if(childList[i].className != "Panel" 
	    	    			&& childList[i].index == this.index) {
			    			ci = i;
			    			break;
			    		}
	    	    	}
	    	    	if(ci < 0) {
	    	    		ci = childList.length - 1;
	    	    	} 
	    	    	var ptop = 0;
	    	    	if(ci > 0) {
			    	    ptop = this._getNumberValue(childList[ci - 1].getControl()[0].style.top,0);
	    	    	}
	    	    	this.thisWindow.style.top = (ptop + parseInt(this.getOption().top * this.getOption().scale, 10)) + "px";
	    	    }
	    	}
	    },
	    
	    refresh:function() {
	    	//边距处理
	    	this._setNumberValue(this.thisWindow, "margin", parseInt(this._margin * this.getOption().scale, 10));
	    	this._setNumberValue(this.thisWindow, "marginLeft", parseInt(this._marginLeft * this.getOption().scale, 10));
	    	this._setNumberValue(this.thisWindow, "marginRight", parseInt(this._marginRight * this.getOption().scale, 10));
	    	this._setNumberValue(this.thisWindow, "marginTop", parseInt(this._marginTop * this.getOption().scale, 10));
	    	this._setNumberValue(this.thisWindow, "marginBottom", parseInt(this._marginBottom * this.getOption().scale, 10));
	    	this._setNumberValue(this.thisWindow, "padding", parseInt(this._padding * this.getOption().scale, 10));
	    	this._setNumberValue(this.thisWindow, "paddingLeft", parseInt(this._paddingLeft * this.getOption().scale, 10));
	    	this._setNumberValue(this.thisWindow, "paddingRight", parseInt(this._paddingRight * this.getOption().scale, 10));
	    	this._setNumberValue(this.thisWindow, "paddingTop", parseInt(this._paddingTop * this.getOption().scale, 10));
	    	this._setNumberValue(this.thisWindow, "paddingBottom", parseInt(this._paddingBottom * this.getOption().scale, 10));
	    	this._setNumberValue(this.thisWindow, "border", this._border);
	    	this._setNumberValue(this.thisWindow, "borderLeft", this._borderLeft);
	    	this._setNumberValue(this.thisWindow, "borderRight", this._borderRight);
	    	this._setNumberValue(this.thisWindow, "borderTop", this._borderTop);
	    	this._setNumberValue(this.thisWindow, "borderBottom", this._borderBottom);
	    	this._setNumberValue(this.thisWindow, "top", parseInt(this.getOption().top * this.getOption().scale, 10));
	    	this._setNumberValue(this.thisWindow, "left", parseInt(this.getOption().left * this.getOption().scale, 10));
	    	
	    	if(this.getOption().borderStyle != null) {
	    	    this.thisWindow.style.borderStyle = this.getOption().borderStyle;
	    	}

	    	if(this.getOption().borderColor != null) {
	    		this.thisWindow.style.borderColor = this.getOption().borderColor;
	    	}	  
	    	if(this.isPecent(this.getOption().width) === false) {
	    		this._setNumberValue(this.thisWindow, "width", parseInt(this.getOption().width * this.getOption().scale, 10));
	    	}	    	
	    	this._setNumberValue(this.thisWindow, "height", parseInt(this.getOption().height * this.getOption().scale, 10));
	    	if(this.getOption().fontFamily != null && this.getOption().fontFamily !== "") {
	    		this.thisWindow.style.fontFamily = this.getOption().fontFamily;
	    	}
	    	if(this.getOption().fontWeight != null && this.getOption().fontWeight !== "") {
	    		this.thisWindow.style.fontWeight = this.getOption().fontWeight;
	    	}
	    	if(this.getOption().fontStyle != null && this.getOption().fontStyle !== "") {
	    		this.thisWindow.style.fontStyle = this.getOption().fontStyle;
	    	}
	    	if(this.getOption().textDecoration != null && this.getOption().textDecoration !== "") {
	    		this.thisWindow.style.textDecoration = this.getOption().textDecoration;
	    	}
	    	if(this.getOption().fontSize != null && this.getOption().fontSize !== "") {
	    		this._setNumberValue(this.thisWindow, "fontSize", parseInt(this.getOption().fontSize * this.getOption().scale, 10));
	    	}	    	
	    	if(this.getOption().textAlign != null && this.getOption().textAlign !== "") {
	    		this.thisWindow.style.textAlign = this.getOption().textAlign;
	    	}
	    	/*
	    	 * releative 时，top 间距不符合要求
	    	 */
	    	this.refreshTop();
	    },	    
	    _afterRender:function() {
	    	this.base();
	    	if(typeof this.refresh == "function") {
	    		this.refresh();	
	    	}
	    	if(this.hasbind("onAfterRender")) {
	    		this.trigger("onAfterRender",[]);
	    	}
	    },
	    setScale:function(value) {
	    	if(typeof value === "string") {
	    		value = value.replace("%","");
	    		value = parseInt(value, 10);
	    	}
	    	if(value <= 0) {
	    		value = 1;
	    	}
	    	if(value > 50) {
	    		value = value / 100;
	    	}
	    	this.getOption().scale = value;
	    	this.refresh();
	    	this._scale = value;
	    },
	    setOption:function(key, value, oldValue) {
	    	if(oldValue != value) {
		    	this.getOption()[key] = value;
		    	this.trigger("onAttribChanged",[this, key, oldValue, value]);
	    	}
	    },
	    /**
	     * 设置属性.
	     */
	    setAttribute:function(AttrAry) {
	    	for(var i = 0, len = AttrAry.length; i < len; i++) {
	    		var ajs = AttrAry[i];
	    		var ov = null;
	    		var tmpName = ajs.name;
	    		if(ajs.name == "left") {
	    			ov = this.getOption().left;
	    			this.getOption().left = ajs.value;
	    		} else if(ajs.name == "top") {
	    			ov = this.getOption().top;
	    			this.getOption().top = ajs.value;
	    		} else if(ajs.name == "width") {
	    			ov = this.getOption().width;
	    			this.getOption().width = ajs.value;
	    		} else if(ajs.name == "height") {
	    			ov = this.getOption().height;
	    			this.getOption().height = ajs.value;
	    		} else {
	    			ov = this.getOption()[ajs.name];
	    		}
	    		this.setOption(ajs.name, ajs.value, ov);
	    		if(tmpName == "lineStyle") {
	    			ajs.name = tmpName;
	    		}
	    	}
	    	this.setFocus();
	    	this.refresh();
	    },
	    getAttribute:function() {
	    	return [{"name":"top","value":this.getOption().top},
	    	        {"name":"left","value":this.getOption().left},
	    	        {"name":"width","value":this.getOption().width},
	    	        {"name":"height","value":this.getOption().height},	    	        
	    	        {"name":"lock","value":this._lock || "center"}
	    	        ];
	    },
	    doFocus:function() {
	    	var selBorder = this.getOption().selectBorder || "#888888 dashed 3px";
	    	this.thisWindow.style.outline = selBorder;
	    	this._selected = true;
	    	if(this.hasbind("onFocus")) {
	    		this.trigger("onFocus",[]);
	    	}	    	
	    },
	    _doBlur:function() {	
	    	this.thisWindow.style.outline = "";
		    this.focus = false;
		    this._selected = false;
	    },
	    setHeight:function(value){
	    	this.base(value, false);
	    },
	    setWidth:function(value){
	    	this.base(value, false);
	    },
	    isPecent:function(value) {
	    	return /^[0-9]+%/.test(value);
	    }
	});
	return qzz.ui.design.base;
});